import java.sql.*;

public class Main {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {

            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8","root","123456");

            //3.编写sql语句
            String sql = "select * from sys_user";

            //4.预处理sql语句...自动补全等号左部代码：.var
            preparedStatement = conn.prepareStatement(sql);

            //5.若有参数，设置参数

            //6.执行sql语句
            resultSet = preparedStatement.executeQuery();

            //7.查询遍历结果、增删改判断成功
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id + " " + name + " " + pass);
            }

        } catch (ClassNotFoundException | SQLException e) {//出现描述的错误时，执行抛出（throw）
            throw new RuntimeException(e);
        } finally {
            try {
                //8.返回视图，关闭资源
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                if (conn != null) conn.close();
            }catch (SQLException e) {
                throw new RuntimeException(e);
            };
        }
    }
    private static void m1(){

    }
}